package com.qf.config;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @ClassName JdbcConfig
 * @Description TODO
 * @Author 86181
 * @Date 2022/8/30 21:12
 * @Version 1.0
 **/
@Configuration//指定当前类是一个配置类 当配置类作为AnnotationConfigApplicationContext
//对象创建参数时,该注解可以不写
//@ComponentScan("com.qf")
@PropertySource(value = "classpath:jdbc.properties")
public class JdbcConfig {
    @Value("${driverClassName}")
    private String driver;
    @Value("${url}")
    private String url;
    @Value("${username}")
    private String userName;
    @Value("${password}")
    private String password;
    @Bean(name = "connection")
    public Connection getConnection(){
        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, userName, password);
            return connection;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


    }
}
